﻿using System;
using System.Collections.Generic;
using System.Linq;

using KamLib.Web.MVC;


namespace KamLib.Web.Bootstrap
{


    #region DropDown

    public class DropDown : MvcElement
    {

        public Link Title;

        public DropDownMenu Menu;
        public bool HasMenu { get { return Menu != null && Menu.Items.yes(); } }


        public delegate DropDown Setter(Action<Link.Setter> title = null, Action<DropDownMenu.Setter> menu = null);

        public DropDown Set(Action<Link.Setter> title = null, Action<DropDownMenu.Setter> menu = null)
        {
            Title = title;
            Menu = menu;
            return this;
        }

        public static implicit operator DropDown(Action<Setter> setterAction)
        {
            var el = new DropDown();
            if (setterAction == null) return el;

            Setter setter = el.Set;
            setterAction(setter);
            return el;
        }

    }


    public static class DropDownHelper
    {

        public static T Title<T>(this T me, Action<Link.Setter> title)
            where T : DropDown
        {
            me.Title = title;
            return me;
        }

        public static DropDown Menu(this DropDown me, Action<DropDownMenu.Setter> menu)
        {
            me.Menu = menu;
            return me;
        }
        public static DropDown Menu(this DropDown me, params Action<DropDownItem.Setter>[] menuItems)
        {
            me.Menu = new DropDownMenu().Items(menuItems);
            return me;
        }

        public static List<DropDown> ToElements(this Action<DropDown.Setter>[] me, List<DropDown> dest = null)
        {
            if (me.no()) return dest;

            var news = me.Select(a => (DropDown)a).ToList();
            if (dest == null)
                dest = news;
            else
                dest.AddRange(news);

            return dest;
        }

    }

    #endregion


    #region DropDownMenu

    public class DropDownMenu : MvcElement
    {

        public List<DropDownItem> Items;


        public delegate DropDownMenu Setter(params Action<DropDownItem.Setter>[] items);


        public DropDownMenu Set(params Action<DropDownItem.Setter>[] items)
        {
            Items = items.ToElements(Items);
            return this;
        }


        public static implicit operator DropDownMenu(Action<Setter> setterAction)
        {
            var el = new DropDownMenu();
            if (setterAction == null) return el;

            Setter setter = el.Set;
            setterAction(setter);
            return el;
        }

    }


    public static class DropDownMenuHelper
    {

        public static DropDownMenu Items(this DropDownMenu me, params Action<DropDownItem.Setter>[] items)
        {
            me.Items = items.ToElements(me.Items);
            return me;
        }

    }

    #endregion


    #region DropDownItem

    public class DropDownItem : Link
    {

        public DropDownMenu Menu;
        public bool HasMenu { get { return Menu != null && Menu.Items.yes(); } }

        public delegate DropDownItem Setter(MvcText title, string url = null, Action<DropDownMenu.Setter> menu = null);

        public DropDownItem Set(MvcText title, string url = null, Action<DropDownMenu.Setter> menu = null)
        {
            base.Set(title, url);
            Menu = menu;
            return this;
        }

        public static implicit operator DropDownItem(Action<Setter> setterAction)
        {
            var el = new DropDownItem();
            if (setterAction == null) return el;

            Setter setter = el.Set;
            setterAction(setter);
            return el;
        }

    }

    public static class DropDownItemHelper
    {

        public static List<DropDownItem> ToElements(this Action<DropDownItem.Setter>[] me, List<DropDownItem> dest = null)
        {
            if (me.no()) return dest;

            var news = me.Select(a => (DropDownItem)a).ToList();
            if (dest == null)
                dest = news;
            else
                dest.AddRange(news);

            return dest;
        }

        public static DropDownItem Menu(this DropDownItem me, Action<DropDownMenu.Setter> menu)
        {
            me.Menu = menu;
            return me;
        }
        public static DropDownItem Menu(this DropDownItem me, params Action<DropDownItem.Setter>[] menuItems)
        {
            me.Menu = new DropDownMenu().Items(menuItems);
            return me;
        }

    }

    #endregion


}